<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改活动公告')" />
    <th:block th:include="include :: summernote-css" />
</head>
<style>
    .progress {
        width: 100%;
        height: 15px;
        border: 1px solid #ccc;
        border-radius: 10px;
        margin: 10px 0px;
        overflow: hidden;
    }
    /* 初始状态设置进度条宽度为0px */
    .progress > div {
        width: 0px;
        height: 100%;
        line-height: 15px;
        text-align: center;
        background-color: yellowgreen;
        transition: all .3s ease;
    }
</style>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form id="upform" enctype='multipart/form-data' style="display:none;">
        <input id="upteainput" name="filePath" class="form-control" type="file">
    </form>
    <form class="form-horizontal m" id="form-activity-edit" th:object="${activity}">
        <a class="btn btn-success" id="uptea"  style="width: 10%">
            <i class="fa fa-upload"></i> 上传视频
        </a>
        <input type="text" class="btn btn-w-m btn-white"  id="url" name="activityViodeo" th:field="*{activityViodeo}" placeholder="未上传视频文件" readonly="true" style="width: 82%;margin-left: 1%">
        <button class="btn btn-info " type="button" style="margin-left: 1%;width: 5%;" onclick="boncopy()">
            <i class="fa fa-paste"></i>复制
        </button>
        <div class="progress">
            <div></div>
        </div>
        <input id="activityId" name="activityId" th:field="*{activityId}"  type="hidden">
        <div class="form-group">
            <label class="col-sm-2 control-label is-required">活动标题：</label>
            <div class="col-sm-10">
                <input id="activityTitle" name="activityTitle" th:field="*{activityTitle}" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">活动内容：</label>
            <div class="col-sm-10">
                <input id="activityContent" name="activityContent" th:field="*{activityContent}" type="hidden">
                <div id="editor" class="summernote"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">活动状态：</label>
            <div class="col-sm-10">
                <div class="radio-box" th:each="dict : ${@dict.getType('sys_activity_status')}">
                    <input type="radio" th:id="${dict.dictCode}" name="status" th:value="${dict.dictValue}" th:field="*{status}">
                    <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: summernote-js" />
<script type="text/javascript">
    var prefix = ctx + "system/activity";

    function boncopy(){
        var e = document.getElementById("url");
        e.select(); // 选择对象
        document.execCommand("Copy"); // 执行浏览器复制命令
    }
    $("#uptea").click(function () {
        $("#upteainput").click();
    });
    //下面是ajax上传文件的代码，此处就不做过多讲解。
    $("#upteainput").change(function () {//如果上传文件的input内容发生了变化
        $val = $("#upteainput").val();
        if ($val != '') {//要上传的文件名不为空
            $data = new FormData($("#upform")[0]);//创建一个formdata对象
            $host = window.location.host;
            $.ajax({
                url: "/common/videoupload",
                type: "POST",
                data: $data,
                processData: false,
                contentType: false,
                dataType: "json",
                xhr: function() {
                    var xhr = new XMLHttpRequest();
                    //使用XMLHttpRequest.upload监听上传过程，注册progress事件，打印回调函数中的event事件
                    xhr.upload.addEventListener('progress', function (e) {
                        // console.log(e);
                        //loaded代表上传了多少
                        //total代表总数为多少
                        // var progressRate = (e.loaded / e.total) * 100 + '%';
                        var progressRate = Math.floor(100 * (event.loaded / event.total)); //已经上传的百分比
                        $('.progress > div').html(progressRate + "%").css("width", progressRate + "%");
                        //通过设置进度条的宽度达到效果
                        // $('.progress > div').html(progressRate).css('width', progressRate);
                    })

                    return xhr;
                },
                error: function () {
                    alert('上传失败！');
                },
                success: function (data) {
                    if (data.code != 0) {
                        alert("上传失败！");
                    }
                    // $('#activityViodeo').val(data.url);
                    $('#url').val(data.url);
                    alert("上传成功!");
                    // alert($('#url').val())
                    // window.location.reload();
                }
            });
        }
    });

    $(function() {
        $('.summernote').summernote({
            placeholder: '请输入活动内容',
            height : 192,
            lang : 'zh-CN',
            followingToolbar: false,
            callbacks: {
                onImageUpload: function (files) {
                    sendFile(files[0], this);
                }
            }
        });
        var content = $("#activityContent").val();
        $('#editor').summernote('code', content);
    });

    // 上传文件
    function sendFile(file, obj) {
        var data = new FormData();
        data.append("file", file);
        $.ajax({
            type: "POST",
            url: ctx + "common/upload",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function(result) {
                if (result.code == web_status.SUCCESS) {
                    $(obj).summernote('editor.insertImage', result.url, result.fileName);
                } else {
                    $.modal.alertError(result.msg);
                }
            },
            error: function(error) {
                $.modal.alertWarning("图片上传失败。");
            }
        });
    }

    $("#form-activity-edit").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var sHTML = $('.summernote').summernote('code');
            $("#activityContent").val(sHTML);
            console.log("活动内容打印："+$("#activityContent").val());
            $.operate.save(prefix + "/edit", $('#form-activity-edit').serialize());
        }
    }
</script>
</body>
</html>
